#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

static char usrdata[]={"usr data!"};

int main(int argc, char *argv[])
{
    int fd, ret;
    char *filename;
    char readbuf[100], writebuf[100];

    filename=argv[1];

    fd=open(filename,O_RDWR);
    if(fd<0){
        printf("open error!\n");
        return -1;
    }

    if(atoi(argv[2])==1){
        ret=read(fd,readbuf,50);
        if(ret>=0){
            printf("read data: %s\n", readbuf);
        }
    }

    if(atoi(argv[2])==2){
        memcpy(writebuf, usrdata, sizeof(usrdata));
        ret=write(fd,writebuf,50);
    }

    ret=close(fd);

    return 0;
}